


package com.sakila.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sakila.dbo.FilmDBO;

import com.sakila.util.DBConnection;


public class FilmDAO implements IFilmDAO {








	  private Logger logger = Logger.getLogger(this.getClass());
	//SimpleDateFormat formatter = new SimpleDateFormat(
		//	"yyyy.MM.dd G 'at' HH:mm:ss z");

	/* (non-Javadoc)
	 * @see com.sakila.dao.IFilmDAO#findById(int)
	 */
	@Override
	public FilmDBO findById(int filmId) {
		FilmDBO dbo = null;
		ResultSet rs = null;
		PreparedStatement statement = null;
		Connection connection = null;
		try {
			connection = DBConnection.getConnection();
			StringBuilder sb = new StringBuilder();
			sb.append("select film_id as filmId, title as title, description as description, release_year as releaseYear, language_id as languageId, original_language_id as originalLanguageId, rental_duration as rentalDuration, rental_rate as rentalRate, length as length, replacement_cost as replacementCost, special_features as specialFeatures, last_update as lastUpdate ");
			sb.append("from film ");
			sb.append("where film_id = " + filmId);
			statement = connection.prepareStatement(sb.toString());
			logger.info("Executing Query Here in FilmDAO: "+ sb.toString());
			rs = statement.executeQuery();
			if (rs.next()) {
				dbo = new FilmDBO();
				      				
				dbo.setFilm_id(rs.getInt("filmId"));
				dbo.setTitle(rs.getString("title"));
				dbo.setDescription(rs.getString("description"));
				dbo.setRelease_year(rs.getDate("releaseYear"));
				dbo.setLanguage_id(rs.getInt("languageId"));
				dbo.setOriginal_language_id(rs.getInt("originalLanguageId"));
				dbo.setRental_duration(rs.getInt("rentalDuration"));
				dbo.setRental_rate(rs.getInt("rentalRate"));
				    
				dbo.setLength(rs.getInt("length"));
				dbo.setReplacement_cost(rs.getInt("replacementCost"));
				dbo.setSpecial_features(rs.getString("specialFeatures"));
				dbo.setLast_update(rs.getDate("lastUpdate"));
				
			}

		} catch (SQLException e) {
			logger.error("Exception in FilmDAO" + e );
			throw new RuntimeException(e);
		} finally {
			DBConnection.close(rs, statement, connection);
		}
		return dbo;
	}

	/* (non-Javadoc)
	 * @see com.sakila.dao.IFilmDAO#findAll()
	 */
	@Override
	public List<FilmDBO> findAll() {
		List<FilmDBO> returnList = null;
		ResultSet rs = null;
		PreparedStatement statement = null;
		Connection connection = null;
		FilmDBO dbo = null;
		try {
			connection = DBConnection.getConnection();
			StringBuilder sb = new StringBuilder();
			sb.append("select film_id as filmId, title as title, description as description, release_year as releaseYear, language_id as languageId, original_language_id as originalLanguageId, rental_duration as rentalDuration, rental_rate as rentalRate, length as length, last_update as lastUpdate,rating as rating, special_features as specialFeatures ");
			sb.append("from film ");
			statement = connection.prepareStatement(sb.toString());
			logger.info("Executing Query Here FilmDAO infindAll: "+ sb.toString());
			rs = statement.executeQuery();
			returnList = new ArrayList<FilmDBO>();
			while (rs.next()) {
				dbo = new FilmDBO();
				dbo.setFilm_id(rs.getInt("filmId"));
				dbo.setTitle(rs.getString("title"));
				dbo.setDescription(rs.getString("description"));
				dbo.setRelease_year(rs.getDate("releaseYear"));
				dbo.setLanguage_id(rs.getInt("languageId"));
				dbo.setOriginal_language_id(rs.getInt("originalLanguageId"));
				dbo.setRental_duration(rs.getInt("rentalDuration"));
				dbo.setRental_rate(rs.getInt("rentalRate"));
				    
				dbo.setLength(rs.getInt("length"));
				/*, replacement_cost as replacementCost, special_features as specialFeatures
				dbo.setReplacement_cost(rs.getInt("replacementCost"));*/
				dbo.setSpecial_features(rs.getString("specialFeatures"));
				dbo.setLast_update(rs.getDate("lastUpdate"));
				dbo.setRating(rs.getString("rating"));
				returnList.add(dbo);
				
			}

		} catch (SQLException e) {
			logger.error("Exception in FilmDAO" + e );
			throw new RuntimeException(e);
		} finally {
			DBConnection.close(rs, statement, connection);
		}
logger.debug("Display Me everyone from List:" + returnList);
		return returnList;
	}
	
}

